package com.honglang.module.user.service;







import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;

import com.honglang.common.tools.Constants;
import com.honglang.mail.impl.MailSender;
import com.honglang.module.user.dao.UserDao;
import com.honglang.module.user.entity.User;

public class UserServiceImpl implements UserService{
	private UserDao userDao;
	
	private MailSender mailSender;
	
	public User createUser(){
		
		
		return null;
	}
	
	
	
	public User saveUser(User user){
		
		return null != user ? userDao.merge(user) : null;
	}
	
	
	/**
	 * Get the user by username;
	 * @param userName
	 * @return
	 */
	public User getUserByUserName(String userName){
		List<User> userList = this.userDao.getUserByUsername(userName);
		
		return userList.size()>0?userList.get(0) : null;
	}
	
	
	
	
	/**
	 * To check the username is exist or not?
	 * @param userName
	 * @return
	 */
	public boolean isUserNameExist(String userName){
		
		return  userDao.getUserByUsername(userName).size()>0? true : false;
	
	}
	
	
	/**
	 * To check the email is exist or not?
	 * @param userName
	 * @return
	 */
	public boolean isEmailExist(String email){
		
		return  userDao.getUserByEmail(email).size()>0? true : false;
	}
	
	
	/**
	 * is user exist
	 * @param user
	 * @return
	 */
	public boolean isUserExist(User user){
		Example example = Example.create(user).excludeProperty("male").excludeProperty("role").excludeZeroes();
		return this.getUserDao().findByExampleQuery(example).size()>0? true:false;
	}
	
	
	/**
	 * validate the user;
	 * @param user
	 * @return
	 */
	public boolean validateUser(User user){
		Example example = Example.create(user).excludeProperty("male").excludeProperty("role").excludeZeroes();
		
		return this.getUserDao().findByExampleQuery(example).size() >0 ? true :false;
		
	}
	
	
	
	
	
	/**
	 * get User by username and email
	 * @param username
	 * @param email
	 * @return
	 */
	public User getUserByNameAndEmail(String username,String email){
		DetachedCriteria dc = DetachedCriteria.forClass(User.class);
		dc.add(Restrictions.eq("email", email))
		  .add(Restrictions.eq("userName", username));
		
		List<User> userList = this.getUserDao().getByDetachedCriteria(dc);
		return userList.size()>0?userList.get(0):null;
		   
	}
	
	
	
	/**
	 * reset user password ,and send it to user email;
	 * @param user
	 */
	public String sendPassword(User user){
		String text = Constants.FORGET_PASSWORD_MAIL_Conent_PRE + user.getPassword();
		return  mailSender.sendMessage(mailSender.createMessage(Constants.MY_EMAIL_USERNAME, Constants.MY_EMAIL_PASSWORD, Constants.FORGET_PASSWORD_MAIL_SUBJECT, user.getEmail(),text ));
	}
	
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public MailSender getMailSender() {
		return mailSender;
	}


	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	
}
